import axios from 'axios';
import query from 'query-string';

const BASE_URL = '/promotion/coupon';


export interface CouponParam {
    name?: string;
    type?: number;
    page?: number;
    size?: number;
    sort?: Array<string>;
}

export interface CouponResponse {
    id?: number;
    name: string;
    subName?: string;
    startTime?: string;
    endTime?: string;
    useThreshold?: string;
    discountAmount?: string;
    discountRate?: string;
    type?: string;
    fullLimited?: string;
    status?: string;

    createdBy?: string;
    updatedBy?: string;
    createdAt?: string;
    updatedAt?: string;
}

export interface CouponSaveCommand {
    id?: number;
    name: string;
    subName?: string;
    startTime?: string;
    endTime?: string;
    useThreshold?: string;
    discountAmount?: string;
    discountRate?: string;
    type?: string;
    fullLimited?: string;
    status?: string;

}

export interface CouponResponseList {
    list: CouponResponse[];
    total: number;
}


// 通过CouponParam参数获取分页列表
export function pageCoupon(params: CouponParam) {
    return axios.get<CouponResponseList>(`${BASE_URL}/page`, {
        params, paramsSerializer: (obj) => {
            return query.stringify(obj);
        }
    })
}

// 通过id获取优惠券信息
export function getCoupon(id: number) {
    return axios.get<CouponResponse>(`${BASE_URL}/${id}`);
    
}

export function listCoupon(params: CouponParam) {
    return axios.get<CouponResponse[]>(`${BASE_URL}/list`, {
        params, paramsSerializer: (obj) => {
            return query.stringify(obj);
        }
    });
}


// 添加优惠券
export function addCoupon(params: CouponSaveCommand) {
    return axios.post(`${BASE_URL}`, params);
}

// 更新优惠券
export function updateCoupon(params: CouponSaveCommand) {
    return axios.put(`${BASE_URL}`, params);
}

// 批量删除优惠券
export function deleteCoupon(ids: number | Array<number>) {
    return axios.delete(`${BASE_URL}/${ids}`);
}